// ==UserScript==
// @name				HWM - Game Menu
// @namespace			figey_hwm_
// @description			Allow show/hide links from main game menu
// @version				0.1.1
// @run-at				document-end
// @grant				GM_deleteValue
// @grant				GM_getValue
// @grant				GM_info
// @grant				GM_listValues
// @grant				GM_notification
// @grant				GM_setValue
// @noframe
// @require				https://notabug.org/figey/localDataStorage/raw/8018b121fb20340d325f45999a308b02b8d761e1/localDataStorage-1.2.0.trim.js
// @require				https://notabug.org/figey/hwm_scripts/raw/59e98a179ba73cc408884238fa4c7c0285622194/src/core.js
// @include				/^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/.+/
// @exclude				/^https?:\/\/(www\.)?(heroeswm\.ru|lordswm\.com)\/(cgame|war|login)\.php.*/
// ==/UserScript==

'use strict';

/**
 * Script settings
 */
const scriptSettings = {
	'gm_tavern': { href:'tavern.php', value:true },
	'gm_roulette': {href:'roulette.php', value:true },
	'gm_stats': { href:'plstats.php', value:true },
	'gm_forum': { href:'forum.php', value:true },
	'gm_chat': { href:'frames.php', value:true }
};

/**
 * Manipulate menu object
 */
const Menu = {
	/**
	 * Main menu bar
	 */
	main: document.querySelector('table[width="580"]'),

	/**
	 * Show or hide menu entry
	 * @param href identify for menu entry
	 * @param visible true if make visible, false for not
	 */
	display(href, visible){
		const link = document.querySelector("a[href*='" + href + "']");
		if( link ){
			const entry = getParentByTagName(link, 'td', 3);
			if( entry ){
				// remember width on first time
				if( !this._width.hasOwnProperty(href) && !visible ){
					this._width[href] = this._size(href);
				}
				entry.style.display = visible ? '' : 'none';
				if( entry.previousElementSibling )
					entry.previousElementSibling.style.display = visible ? '' : 'none';

				this.main.width = (+this.main.width + (visible ? 1 : -1 )*this._width[href]).toString();
			}
		}
	},

	/**
	 * Get link text
	 * @param href identify for menu entry
	 * @return link name or empty string
	 */
	name(href){
		const link = document.querySelector("a[href*='" + href + "']");
		if( link && link.childElementCount == 1 ){
			return link.firstChild.textContent;
		}
		return '';
	},

	/**
	 * Array for store entry width
	 */
	_width: {},

	_size(href){
		const link = document.querySelector("a[href*='" + href + "']");
		let width = 0;
		if( link ){
			const entry = getParentByTagName(link, 'td', 3);
			if( entry ){
				width += entry.offsetWidth;
				if( entry.previousElementSibling )
					width += entry.previousElementSibling.offsetWidth;
			}
		}
		return width;
	}
};

/**
 * init
 */
SettingsTab.init();
const options = new GMSettings(scriptSettings);
const tab = SettingsTab.add('Menu', 'Меню');

/**
 * Hide unchecked elements
 */
Object.keys(options.data).forEach(function(key){
	if( !options.data[key].value ){
		Menu.display(options.data[key].href, options.data[key].value);
	}
});

/**
 * Fill settings tab
 */
Object.keys(options.data).forEach(function(key){
	const input = document.createElement('input');
	input.id = key;
	input.type = 'checkbox';
	input.checked = options.data[key].value;
	input.addEventListener('change', function(event){
		options.data[event.target.id].value = event.target.checked;
		options.data[event.target.id].save();
		Menu.display(options.data[event.target.id].href, options.data[event.target.id].value);
	});

	const label = document.createElement('label');
	label.htmlFor = key;
	label.textContent = Menu.name(options.data[key].href);

	tab.appendChild(input);
	tab.appendChild(label);
	tab.appendChild(document.createElement('br'));
});
